package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SecurityStaffCertificateRecord;
import com.ruoyi.system.entity.SecurityStaffCertificateRecordFile;
import com.ruoyi.system.service.SecurityStaffCertificateRecordFileService;
import com.ruoyi.system.service.SecurityStaffCertificateRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 安全_企业基本信息_员工证书档案
 *
 * @since 2022-11-18 09:31:26
 */
@RestController
@RequestMapping("/securityStaffCertificateRecord")
public class SecurityStaffCertificateRecordController extends BaseController {
    @Resource
    private SecurityStaffCertificateRecordService securityStaffCertificateRecordService;
    @Resource
    private SecurityStaffCertificateRecordFileService securityStaffCertificateRecordFileService;


    /**
     * 分页查询
     *
     * @param staffCertificateRecord 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SecurityStaffCertificateRecord staffCertificateRecord) {
        startPage();
        List<SecurityStaffCertificateRecord> staffCertificateRecordList = this.securityStaffCertificateRecordService.queryByPage(staffCertificateRecord);
        return getDataTable(staffCertificateRecordList);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.securityStaffCertificateRecordService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param staffCertificateRecord 实体
     * @return 新增结果
     */
    @PostMapping("/create")
   // @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:create')")
    public AjaxResult add(@RequestBody SecurityStaffCertificateRecord staffCertificateRecord) {
        return AjaxResult.success(this.securityStaffCertificateRecordService.insert(staffCertificateRecord));
    }

    /**
     * 编辑数据
     *
     * @param staffCertificateRecord 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
  //  @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:update')")
    public AjaxResult edit(@RequestBody SecurityStaffCertificateRecord staffCertificateRecord) {
        return AjaxResult.success(this.securityStaffCertificateRecordService.update(staffCertificateRecord));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
 //   @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.securityStaffCertificateRecordService.deleteById(id));
    }

    /**
     * 文件上传
     *
     * @return 上传结果
     */
    @PostMapping("/fileUpload")
  //  @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:fileUpload')")
    public AjaxResult uploadAcceptance(@RequestBody SecurityStaffCertificateRecord staffCertificateRecord) {
        if (null == staffCertificateRecord) {
            return AjaxResult.error("参数异常！");
        }
        if (null == staffCertificateRecord.getId()) {
            return AjaxResult.error("参数异常！");
        }
        if (CollectionUtils.isEmpty(staffCertificateRecord.getRecordFileList())) {
            return AjaxResult.error("参数异常！");
        }
        StringBuilder key = new StringBuilder();
        if (!CollectionUtils.isEmpty(staffCertificateRecord.getRecordFileList())) {
            for (SecurityStaffCertificateRecordFile staffCertificateRecordFile : staffCertificateRecord.getRecordFileList()) {
                securityStaffCertificateRecordFileService.insert(staffCertificateRecordFile);
                key.append(staffCertificateRecordFile.getId());
                key.append(",");
            }
        }
        if (!StringUtils.isEmpty(key.toString())) {
            SecurityStaffCertificateRecord staffRecord = new SecurityStaffCertificateRecord();
            staffRecord.setId(staffCertificateRecord.getId());
            staffRecord.setRecordRecordFileId(key.toString());
            securityStaffCertificateRecordService.update(staffRecord);
        }
        return AjaxResult.success();
    }


    @Log(title = "证书照片文件(", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
  //  @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:exportData')")
    public void export(HttpServletResponse response, @RequestBody Integer[] ids) {
        List<SecurityStaffCertificateRecord> promiseList = new ArrayList<>();
        List<Integer> list = new ArrayList<>(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(list)) {
            for (Integer id : list) {
                SecurityStaffCertificateRecord promise = securityStaffCertificateRecordService.queryById(id);
                promiseList.add(promise);
            }
        }
        /*if (!CollectionUtils.isEmpty(promiseList)) {
            promiseList.forEach(val -> {
               if (null != val.getTaskState()) {
                    if (val.getTaskState() == 0) {
                        val.setTaskStateBase("待处理");
                    }
                    if (val.getTaskState() == 1) {
                        val.setTaskStateBase("已处理");
                    }
                }
                if (StringUtils.isNotEmpty(val.getSafetyAcceptorId())) {
                    SysUser sysUser = iSysUserService.selectUserById(Long.valueOf(val.getSafetyAcceptorId()));
                    if (null != sysUser) {
                        val.setNickName(sysUser.getNickName());
                        if (null != sysUser.getDeptId()) {
                            SysDept sysDept = iSysDeptService.selectDeptById(sysUser.getDeptId());
                            if (null != sysDept) {
                                val.setDeptName(sysDept.getDeptName());
                            }
                        }
                    }
                }
            });
        }*/
        ExcelUtil<SecurityStaffCertificateRecord> util = new ExcelUtil<>(SecurityStaffCertificateRecord.class);
        util.exportExcel(response, promiseList, "证书照片文件(");
    }

    @Log(title = "安全_企业基本信息_员工证书档案", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
   // @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:importData')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SecurityStaffCertificateRecord> util = new ExcelUtil<>(SecurityStaffCertificateRecord.class);
        List<SecurityStaffCertificateRecord> securityStaffCertificateRecords = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = securityStaffCertificateRecordService.importExcel(securityStaffCertificateRecords, updateSupport, operName);
        return success(message);
    }

    @Log(title = "安全_企业基本信息_员工证书档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
  //  @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:export')")
    public void export(HttpServletResponse response, SecurityStaffCertificateRecord securityStaffCertificateRecord) {
        List<SecurityStaffCertificateRecord> list = securityStaffCertificateRecordService.queryByPage(securityStaffCertificateRecord);
        ExcelUtil<SecurityStaffCertificateRecord> util = new ExcelUtil<>(SecurityStaffCertificateRecord.class);
        util.exportExcel(response, list, "安全_企业基本信息_员工证书档案");
    }

    @PostMapping("/importTemplate")
  //  @PreAuthorize("@ss.hasPermi('securityStaffCertificateRecord:importTemplate')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SecurityStaffCertificateRecord> util = new ExcelUtil<>(SecurityStaffCertificateRecord.class);
        util.importTemplateExcel(response, "安全_企业基本信息_员工证书档案");
    }

    @GetMapping("/sendTest")
    public void sendTest() {
        securityStaffCertificateRecordService.checkCertificatePeriod();
    }
}

